package br.ucb.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.ucb.servicos.ResourceBundleUtil;
public class FactoryConnection {

	private FactoryConnection() {

	}

	private static FactoryConnection fabrica;

	public static FactoryConnection getInstance() {
		if(fabrica==null)
			fabrica=new FactoryConnection();
		return fabrica;
	}

	public Connection criaConexao() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/daocesb";
		String usuario = ResourceBundleUtil.getPropriedade("USUARIO");
		String senha = ResourceBundleUtil.getPropriedade("SENHA");
		Connection conexao = null;

		conexao = DriverManager.getConnection(url,usuario,senha);


		return conexao;

	}
	public Connection criaConexaoFirst() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/";
		String usuario = ResourceBundleUtil.getPropriedade("USUARIO");
		String senha = ResourceBundleUtil.getPropriedade("SENHA");
		Connection conexao = null;

		conexao = DriverManager.getConnection(url,usuario,senha);

		return conexao;

	}
	public void closeConexao(Connection con){
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public void closeConexao(Connection con, PreparedStatement stm){

		try {
			stm.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void closeConexao(Connection con, PreparedStatement stm, ResultSet rs){
		try {
			rs.close();
			stm.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}